package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IByJzService;

import javax.servlet.http.HttpServletRequest;

/**
 * 家长认证Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-21
 */
@Service
public class ByJzServiceImpl implements IByJzService {
    @Autowired
    private ByJzMapper byJzMapper;

    @Autowired
    private ByCodeMsgMapper codeMsgMapper;

    @Autowired
    private ByParamMapper paramMapper;

    @Autowired
    private ByJzYhqMapper byJzYhqMapper;

    @Autowired
    private DateUtilSqlMapper dateUtilSqlMapper;

    @Autowired
    private ByWxUserMapper wxUserMapper;

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private ByOrderPayLogMapper orderPayLogMapper;

    @Autowired
    private ByCouponUserMapper byCouponUserMapper;

    @Override
    public void stopJzYhqStatus() {
        List<ByJz> list = byJzMapper.stopJzYhqList();
        for(ByJz jz:list){
            jz.setQid("0");
            jz.setZk("100");
            jz.setQstatus("0");
            jz.setYhqid("0");

            byJzMapper.updateByJz(jz);
        }
    }

    @Override
    public Object payYhkqOrder(JSONObject json, HttpServletRequest request) {
        String id = json.getString("id");
        String wxid = json.getString("wxid");
        String orderSn = IdUtils.simpleUUID(); // 进行生成订单编号
        ByParam pEntity = paramMapper.selectByParamById(Long.parseLong(id));


        ByJzYhq jzYhq = new ByJzYhq();
        jzYhq.setQid(id);
        jzYhq.setWxid(wxid);
        jzYhq.setPayid(orderSn);
        jzYhq.setQname(pEntity.getRemark());
        jzYhq.setMoney(pEntity.getVal());
        jzYhq.setStatus("0");
        jzYhq.setAddtime(dateUtilSqlMapper.getNowDateTime());
        jzYhq.setUptime(dateUtilSqlMapper.getNowDateTime());
        if(id.equals("15")){
            jzYhq.setZkl("94");
            jzYhq.setDays("30");
        }
        if(id.equals("16")){
            jzYhq.setZkl("92");
            jzYhq.setDays("90");
        }
        if(id.equals("17")){
            jzYhq.setZkl("90");
            jzYhq.setDays("180");
        }
        byJzYhqMapper.insertByJzYhq(jzYhq);

        // 进行生成支付日志_____进行保存_____进行保存优惠券ID
        ByOrderPayLog orderPayLog = new ByOrderPayLog();
        orderPayLog.setOrderSn(orderSn);
        orderPayLog.setPrice(pEntity.getVal());
        orderPayLog.setAddtime(dateUtilSqlMapper.getNowDateTime());
        orderPayLog.setOrderId("yhq"+id);
        orderPayLog.setWxId(wxid);
        orderPayLogMapper.insertByOrderPayLog(orderPayLog);


        ByWxUser userEntity = wxUserMapper.selectByWxUserById(wxid);
        WxPayMpOrderResult result = null;
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setOutTradeNo(orderSn);
            orderRequest.setOpenid(userEntity.getWeixinOpenid());
            orderRequest.setBody("订单：" + orderSn);
            // 元转成分
            int fee = 0;
            BigDecimal actualPrice = BigDecimal.valueOf(Double.parseDouble(pEntity.getVal()));
            fee = actualPrice.multiply(new BigDecimal(100)).intValue();
            orderRequest.setTotalFee(fee);
            orderRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
            result = wxPayService.createOrder(orderRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Map<String,Object> getParamByHyk() {
        Map<String,Object> map = new HashMap<>();
        JSONObject yp = paramMapper.getParamJSONObjectById("15");
        JSONObject jp = paramMapper.getParamJSONObjectById("16");
        JSONObject np = paramMapper.getParamJSONObjectById("17");
        map.put("yp",yp);
        map.put("jp",jp);
        map.put("np",np);
        return map;
    }

    @Override
    public JSONObject getDetailBywxid(JSONObject json) {
        String wxid = json.getString("wxid");
        JSONObject entity = byJzMapper.getDetailBywxid(wxid);
        return entity;
    }

    @Override
    public JSONObject managSave(ByJz entity) {
        JSONObject json = new JSONObject();
        /***********************************************/
        String cardno = entity.getCardno();//获取证件号码，判断当前人员是否存在
        /*List<ByBs> list = byJzMapper.queryCountByCardNo(cardno);
        if(list.size() > 0){
            json.put("code","400");
            json.put("msg","当前帮手已经存在！");
            return json;
        }*/
        //进行验证短信问题
        String mobile = entity.getPhone();
        String code = entity.getCode();
        List<ByCodeMsg> listMsg = codeMsgMapper.queryByCodeAndMobile(mobile, code);
        if(listMsg.size() == 0){
            json.put("code","400");
            json.put("msg","短信验证码输入错误，请检查！");
            return json;
        }
        entity.setAddtime(DateUtils.getTime());
        entity.setUpdatetime(DateUtils.getTime());

        insertByJz(entity);
        json.put("code","200");
        return json;
    }

    /**
     * 查询家长认证
     *
     * @param id 家长认证主键
     * @return 家长认证
     */
    @Override
    public ByJz selectByJzById(Long id)
    {
        return byJzMapper.selectByJzById(id);
    }

    /**
     * 查询家长认证列表
     *
     * @param byJz 家长认证
     * @return 家长认证
     */
    @Override
    public List<ByJz> selectByJzList(ByJz byJz)
    {
        return byJzMapper.selectByJzList(byJz);
    }

    /**
     * 新增家长认证
     *
     * @param byJz 家长认证
     * @return 结果
     */
    @Override
    public int insertByJz(ByJz byJz)
    {
        return byJzMapper.insertByJz(byJz);
    }

    /**
     * 修改家长认证
     *
     * @param byJz 家长认证
     * @return 结果
     */
    @Override
    public int updateByJz(ByJz byJz)
    {
        return byJzMapper.updateByJz(byJz);
    }

    /**
     * 批量删除家长认证
     *
     * @param ids 需要删除的家长认证主键
     * @return 结果
     */
    @Override
    public int deleteByJzByIds(Long[] ids)
    {
        return byJzMapper.deleteByJzByIds(ids);
    }

    /**
     * 删除家长认证信息
     *
     * @param id 家长认证主键
     * @return 结果
     */
    @Override
    public int deleteByJzById(Long id)
    {
        return byJzMapper.deleteByJzById(id);
    }

    /**
     * 获取家长优惠券信息
     * @param userid
     * @return
     */
    @Override
    public List<JSONObject> getyhq(String userid) {
        List<JSONObject> result = byCouponUserMapper.getYhq(userid);
        return result;
    }

    @Override
    public List<JSONObject> Zfyhq(String wxid) {
        List<JSONObject> result = byCouponUserMapper.Zfyhq(wxid);
        return result;
    }
}
